package com.cake.liulanxiangzhu.service.impl;

import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.mapper.AdminCategoryMapper;
import com.cake.liulanxiangzhu.pojo.dto.AdminCategoryDTO;
import com.cake.liulanxiangzhu.pojo.dto.AdminCategoryInsertDTO;
import com.cake.liulanxiangzhu.pojo.entity.AdminCategory;
import com.cake.liulanxiangzhu.pojo.vo.AdminCategoryVO;
import com.cake.liulanxiangzhu.service.ICategoryService;
import com.cake.liulanxiangzhu.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    AdminCategoryMapper mapper;

    @Override
    public void addNew(AdminCategoryInsertDTO adminCategoryInsertDTO) {
        System.out.println("adminCategoryDTO = " + adminCategoryInsertDTO);
        List<AdminCategoryVO> list = mapper.list();
        for (AdminCategoryVO vo : list) {
            if (adminCategoryInsertDTO.getCategoryName().equals(vo.getCategoryName())) {
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "分类名不能相同");

            }
        }
        AdminCategory adminCategory = new AdminCategory();
        BeanUtils.copyProperties(adminCategoryInsertDTO, adminCategory);
        System.out.println("adminCategory = " + adminCategory);
        LocalDateTime time = LocalDateTime.now();
        adminCategory.setCreatTime(time);
        adminCategory.setUpdateTime(time);
        mapper.insert(adminCategory);
    }

    @Override
    public void delete(Integer categoryId) {
        System.out.println("categoryId = " + categoryId);
        AdminCategoryVO vo = mapper.getCategoryByID(categoryId);
        if (vo != null) {
            String categoryIcon = vo.getCategoryIcon();
            new File("e:/files" + categoryIcon).delete();
            int delete = mapper.delete(categoryId);
            if (delete != 1) {
                throw new ServiceException(ServiceCode.ERR_DELETE, "服务器忙，请稍后再试");
            }
        } else {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "访问数据不存在");
        }
    }

    @Override
    public void update(AdminCategoryDTO adminCategoryDTO) {
        System.out.println("adminCategoryDTO = " + adminCategoryDTO);
        AdminCategoryVO vo = mapper.getCategoryByID(adminCategoryDTO.getCategoryId());
        if (vo != null) {
            if (!adminCategoryDTO.getCategoryIcon().isEmpty()) {
                if (!adminCategoryDTO.getCategoryIcon().equals(vo.getCategoryIcon())){
                    String categoryIcon = vo.getCategoryIcon();
                    new File("e:/files" + categoryIcon).delete();
                }
            }
            AdminCategory adminCategory = new AdminCategory();
            BeanUtils.copyProperties(adminCategoryDTO, adminCategory);
            adminCategory.setUpdateTime(LocalDateTime.now());
            int update = mapper.update(adminCategory);
            if (update != 1) {
                throw new ServiceException(ServiceCode.ERR_DELETE, "服务器忙，请稍后再试");
            }
        } else {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "没有分类数据");
        }
    }

    @Override
    public List<AdminCategoryVO> list() {
        List<AdminCategoryVO> list = mapper.list();
        if (list == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "没有分类数据");
        } else {
            return list;
        }
    }

    @Override
    public AdminCategoryVO selectById(Integer categoryId) {
        AdminCategoryVO vo = mapper.getCategoryByID(categoryId);
        if (vo != null) {
            return vo;
        } else {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "访问数据不存在");
        }
    }
}
